<?php

namespace app\admin\controller;

use app\common\model\Shops;
use app\common\model\Area;
use app\admin\validate\Configure as ConfigureValidate;

class Configure extends Base
{
	public function __construct()
	{
		parent::__construct();
		$this->isLogin();
	}

	/**
	 * @title 配置列表
	 * @return mixed
	 */
	public function ConfigureList()
	{
		if($this->_shopid){
			$this->error('您没有权限');
		}
		$tvar = array(
			"title" => "配置管理",
			"time" => time(),
			"list" => [
				['name'=>'总部店铺管理','url'=>'BoxShop']
			],
		);
//		print_r($tvar);die;
		$this->assign("data", $tvar);
		return $this->fetch('configure/list');
	}

	/**
	 * @title 总部店铺配置
	 * @return mixed
	 */
	public function BoxShop()
	{
		$ShopsModel = new Shops();

		if (request()->isAjax()) {
			$param = input();
			$Validate = new ConfigureValidate();
			$res = $Validate->scene('BoxShop')->check($param);
			if(!$res){
				$this->error($Validate->getError());
			}

			$rs = $ShopsModel->save($param, [
				'shop_id' => 0,
			]);
			if ($rs === false) {
				$this->error('操作失败');
			}
			$this->success('操作成功','/admin/ConfigureList');
		}

		$data = $ShopsModel->field('*')->where([
			'shop_id' => 0,
		])->find()->toArray();
//		print_r($data);die;

		$AreaModel = new Area();
		$provincelists = $AreaModel->get_list(array("level" => 1),'id,name');
		//市
		$Citylists = $AreaModel->get_list(array("pid" => $data['province_id']),'id,name');
		//区/县
		$Arealists = $AreaModel->get_list(array("pid" => $data['city_id']),'id,name');
		// 街道
		$Streetlists = $AreaModel->get_list(array("pid" => $data['area_id']),'id,name');

		$tvar = array(
			"title" => "管理配置",
			"time" => time(),
			"data" => $data,
			"provincelists" => $provincelists,
			"Citylists" => $Citylists,
			"Arealists" => $Arealists,
			"Streetlists" => $Streetlists,
		);
//		print_r($tvar);die;
		$this->assign("data", $tvar);
		return $this->fetch('configure/boxshop');
	}
}